package lanou3g.myfirstapp36kr.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import cn.bmob.v3.BmobUser;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.base.BaseActivity;
import lanou3g.myfirstapp36kr.ChangeButtonListener;
import lanou3g.myfirstapp36kr.fragment.FindFragment;
import lanou3g.myfirstapp36kr.fragment.MessageFragment;
import lanou3g.myfirstapp36kr.fragment.FragmentMoney;
import lanou3g.myfirstapp36kr.fragment.MyFragment;
import lanou3g.myfirstapp36kr.fragment.Fragmentnews;

public class MainActivity extends BaseActivity implements OnClickListener ,ChangeButtonListener {
    private Button button_news,button_money,button_find,button_message,button_my;
    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
    @Override
    protected void initView() {
        button_news = (Button) findViewById(R.id.btn_news);
        button_money = (Button) findViewById(R.id.btn_money);
        button_find = (Button) findViewById(R.id.btn_find);
        button_message = (Button) findViewById(R.id.btn_message);
        button_my = (Button) findViewById(R.id.btn_my);
    }
    @Override
    protected void initData() {
        button_news.setOnClickListener(this);
        button_money.setOnClickListener(this);
        button_find.setOnClickListener(this);
        button_message.setOnClickListener(this);
        button_my.setOnClickListener(this);
        button_news.setSelected(true);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragmentnews fragmentnews = new Fragmentnews();
        ft.replace(R.id.framlayout_replace,fragmentnews);
        ft.commit();
    }

    @Override
    public void onClick(View view) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()){
            case R.id.btn_news:
                Fragmentnews fragmentnews = new Fragmentnews();
                ft.replace(R.id.framlayout_replace,fragmentnews);
                ft.commit();
                button_news.setSelected(true);
                button_message.setSelected(false);
                button_money.setSelected(false);
                button_find.setSelected(false);
                button_my.setSelected(false);

                break;
            case R.id.btn_money:
                button_money.setSelected(true);
                FragmentMoney fragmentMoney = new FragmentMoney();
                ft.replace(R.id.framlayout_replace,fragmentMoney);
                ft.commit();
                button_news.setSelected(false);
                button_message.setSelected(false);

                button_find.setSelected(false);
                button_my.setSelected(false);

                break;
            case R.id.btn_find:
                button_find.setSelected(true);
                FindFragment findFragment = new FindFragment();
                ft.replace(R.id.framlayout_replace, findFragment);
                ft.commit();
                button_news.setSelected(false);
                button_message.setSelected(false);
                button_money.setSelected(false);

                button_my.setSelected(false);
                break;
            case R.id.btn_message:
                BmobUser bmobUser = BmobUser.getCurrentUser();
                if (bmobUser != null){
                    MessageFragment messageFragment = new MessageFragment();
                    ft.replace(R.id.framlayout_replace, messageFragment);
                    ft.commit();
                    button_message.setSelected(true);
                    button_news.setSelected(false);
                    button_money.setSelected(false);
                    button_find.setSelected(false);
                    button_my.setSelected(false);
                }else {
                    Intent intent = new Intent(this,LoginActivity.class);
//                    startActivity(intent);
                    startActivityForResult(intent,100);

                }

                break;
            case R.id.btn_my:
                button_my.setSelected(true);
                MyFragment myFragment = new MyFragment();
                ft.replace(R.id.framlayout_replace, myFragment);
                ft.commit();
                button_news.setSelected(false);
                button_message.setSelected(false);
                button_money.setSelected(false);
                button_find.setSelected(false);

                break;
        }

    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(resultCode  == -1){
//
//        }
//    }

    @Override
    public void changeButton() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        button_money.setSelected(true);
        FragmentMoney fragmentMoney = new FragmentMoney();
        ft.replace(R.id.framlayout_replace,fragmentMoney);
        ft.commit();
        button_news.setSelected(false);
        button_message.setSelected(false);

        button_find.setSelected(false);
        button_my.setSelected(false);


    }
}
